import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import present from './views/present.vue'
import user from './views/user.vue'
import reg from './views/reg.vue'
import login from './views/login.vue'
import list from './views/list.vue'
import Detail from './views/Detail.vue'
import car from './views/car.vue'
import { isLogin } from './utils/auth'
import ListNav from './views/ListNav.vue'
import searchpage from './views/searchpage.vue'
import find from './views/find.vue'

//注册
import UserCenter from './views/UserCenter.vue'
import UserCenterCG from './views/UserCenterCG.vue'
import Login from './views/login.vue'
import Reg from './views/reg.vue'
import Set from './views/Set.vue'
import SetLogin from './views/SetLogin.vue'
import News from './views/News.vue'
import PerSonal from './views/PerSonal.vue'
import NickName from './views/NickName.vue'



//引入product_list列表
import rou from './product_list/rou.vue'
import guo from './product_list/guo.vue'
import baojian from './product_list/baojian'
import cha from './product_list/cha'
import jiaju from './product_list/jiaju'
import jinkou from './product_list/jinkou'
import jiu from './product_list/jiu'
import meizhuang from './product_list/meizhuang'
import muying from './product_list/muying'
import shuma from './product_list/shuma'
import xiuxian from './product_list/xiuxian'
import you from './product_list/you'
import youxuan from './product_list/youxuan'
import lengcang from './product_list/lengcang'





Vue.use(Router)

const router = new Router({
  routes: [{
    path: '/find',
    name: 'find',
    component: find,
  },
  {
    path: '/present',
    name: 'present',
    component: present,
  }, {
    path: '/usercenter',
    name: 'UserCenter',
    component: UserCenter,
  }, {
    path: '/usercentercg',
    name: 'UserCenterCG',
    component: UserCenterCG,
  }, {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      hideNav: true
    }
  }, {
    path: '/reg',
    name: 'Reg',
    component: Reg,
    meta: {
      hideNav: true
    }
  }, {
    path: '/set',
    name: 'Set',
    component: Set,
    meta: {
      hideNav: true
    }
  }, {
    path: '/setlogin',
    name: 'SetLogin',
    component: SetLogin,
    meta: {
      hideNav: true
    }
  }, {
    path: '/news',
    name: 'News',
    component: News,
    meta: {
      hideNav: true
    }
  }, {
    path: '/personal',
    name: 'PerSonal',
    component: PerSonal,
    meta: {
      hideNav: true
    }
  }, {
    path: '/nickname',
    name: 'NickName',
    component: NickName,
    meta: {
      hideNav: true
    }
  },
  {
    path: 'searchpage',
    name: 'Searchpage',
    component: searchpage,
  },
  {
    path: '/',
    name: 'home',
    component: Home,
  }, {
    path: '/car',
    name: 'car',
    component: car,
  }, {
    path: '/reg',
    name: 'reg',
    component: reg
  }, {
    path: '/login',
    name: 'login',
    component: login
  }, {
    path: '/list',
    name: 'list',
    component: list
  }, {
    path: '/list/:id',
    name: 'Detail',
    component: Detail
  }, {
    path: '/user',
    name: 'user',
    component: user
  },
  {
    path: '/ListNav',
    name: 'ListNav',
    component: ListNav,
    children: [
      {
        path: 'rou',
        name: 'rou',
        component: rou
      },
      {
        path: 'guo',
        name: 'guo',
        component: guo
      },
      {
        path: 'baojian',
        name: 'baojian',
        component: baojian
      },
      {
        path: 'cha',
        name: 'cha',
        component: cha
      },
      {
        path: 'jiaju',
        name: 'jiaju',
        component: jiaju
      },
      {
        path: 'jinkou',
        name: 'jinkou',
        component: jinkou
      },
      {
        path: 'jiu',
        name: 'jiu',
        component: jiu
      },
      {
        path: 'meizhuang',
        name: 'meizhuang',
        component: meizhuang
      },
      {
        path: 'muying',
        name: 'muying',
        component: muying
      },
      {
        path: 'shuma',
        name: 'shuma',
        component: shuma
      },
      {
        path: 'xiuxian',
        name: 'xiuxian',
        component: xiuxian
      },
      {
        path: 'you',
        name: 'you',
        component: you
      },
      {
        path: 'youxuan',
        name: 'youxuan',
        component: youxuan
      },
      {
        path: 'lengcang',
        name: 'lengcang',
        component: lengcang
      },

    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
  }
  ]
})
//brforeEach路由的钩子函数，在每一个路由跳转之前执行
router.beforeEach(function (to, from, next) {
  if (to.meta.needLogin) {
    if (isLogin(
    )) {
      next();
    } else {
      next({
        name: 'login'
      })
    }
  }
  if (to.name == "UserCenterCG") {
    if (localStorage.getItem('userName')) {
      next()
    } else {
      next({
        name: 'UserCenter'
      })
    }
  } else {
    next()
  }
  next();
})

export default router;